$(document).ready(function(){
	  /*display lang*/
	   var displayLang = $("#display-lang").val();
	   
	   /*leftnav */
	   $("a.pflang").addClass("nav-select");
	   $(".leftnav a").mouseover(function(){
		   $(this).addClass("bh");
	   });
	   $(".leftnav a").mouseout(function(){
		   $(this).removeClass("bh");
	   }); 
	   $(".leftnav a").click(function(){
		   if(!$(this).hasClass("nav-select")){
			   $(".leftnav a.nav-select").each(function(){
				   $(this).removeClass("nav-select");
			   });
			   $(this).addClass("nav-select");
			   if($(this).hasClass("c")){
				   $(".lang-display-set").hide();
				   $(".lang-prefer-set").show();
			   }else{
				   $(".lang-display-set").show();
				   $(".lang-prefer-set").hide();
			   }
		   }
	   });
	   
	   
	   $("#preferLangbuttons .sbutton").click(function(){
		   var langs = $("input[name=prefer-langs]:checked");
		   if(langs.length==0){
			   if(displayLang=="en"){
   				$.messager.alert('Error','Please select at least one language!','error');
   			  }else if(displayLang=="zh"){
   				  $.messager.alert('错误','至少需要选择一个语言!','error');
   			  }
		   }else{
			   var array = new Array();
			   for(i=0;i<langs.length;i++){
				   array.push($(langs[i]).val());
			   }
			   storePreferLangs(array);
			   if(displayLang=="en"){
				    var div = $("<div class='messageInfo messager-body'><div class='messager-icon messager-info'></div></div>");
				    var infodiv = $("<div>Your setting has been saved!</div>");
	   				div.append(infodiv);
	   				$("body").append(div);
	   			}else if(displayLang=="zh"){
	   			 var div = $("<div class='messageInfo messager-body'><div class='messager-icon messager-info'></div></div>");
				    var infodiv = $("<div>你的设置已被保存!</div>");
	   				div.append(infodiv);
	   				$("body").append(div);
	   			}
			    if(langs.length==1&&$(langs[0]).val()!=displayLang){
			    	var location = "/sview/account.jsp";
			    	if($(langs[0]).val()=="zh"){
			    		location +="?lang=zh";
			    	}
			    	setTimeout(function(){
			    		window.location = location;
			    	},500);
			    }else{
			    	setTimeout(function(){
			    		window.location.reload();
			    	},500);
			    }
		   }
	   });
});

function storePreferLangs(langs){
	var user  = getUser();
	var data = {};
	data["userName"] = user;
	data["langs"] = langs;
	data = JSON.stringify(data);
	data = ("preferLangs="+encodeURIComponent(data));
	$.ajax({
		url:"/sview/setting",
		async : "true",
		data : data,
		type : "post"
	});
}